package sbc.corso.shareable;

import java.util.Vector;

import corso.lang.CorsoConnection;
import corso.lang.CorsoData;
import corso.lang.CorsoDataException;
import corso.lang.CorsoException;
import corso.lang.CorsoShareable;
import corso.lang.CorsoVarOid;

public class Sensoren implements CorsoShareable { 
	
	private Vector sensorenOids;

	
	public Sensoren (CorsoConnection con) {
		sensorenOids = new Vector();
	}	

	public Sensoren (CorsoVarOid oid) {
	    //System.out.println ("Read SensorenOids = " + oid);
	    try {
            oid.setTimeStamp (0);
	        oid.readShareable(this, null, CorsoConnection.INFINITE_TIMEOUT);
	    }
	    catch (CorsoException e) {
	        e.printStackTrace();
	    }
	}
	
	
	public void addSensor(CorsoConnection con, Sensor sensor) {
		CorsoVarOid oid;
	    try {
            oid = con.createVarOid (con.getCurrentStrategy ());
            oid.writeShareable (sensor, CorsoConnection.INFINITE_TIMEOUT);
            sensorenOids.add(oid);
            //System.out.println("addSensor id: " + sensor.getId());
            //System.out.println("addPlatine oid: " + oid);
	    }
	    catch (CorsoException ce) {   
	        System.out.println (ce);
	        System.exit (-1);
	    }
	}

	public Vector getSensorenOids() {
		return sensorenOids;
	}

	public void read(CorsoData data) throws CorsoDataException {
        StringBuffer structName = new StringBuffer ();
        int arity = data.getStructTag (structName);

        sensorenOids = new Vector ();
        for (int i = 0; i < arity; i++) {
            CorsoVarOid oid = new CorsoVarOid ();
            data.getShareable (oid);
            //System.out.println ("Naechste sensorenOids = " + oid);
            sensorenOids.add(oid);
        }
	}

	public void write(CorsoData data) throws CorsoDataException {
		data.putStructTag ("Sensoren", sensorenOids.size());
        for (int i = 0; i < sensorenOids.size (); i++)
            data.putShareable ((CorsoVarOid) sensorenOids.get (i));
        //System.out.println("Sensoren written");
	}

}
